package gson.Test;

import android.util.Log;

import com.google.gson.JsonArray;

import java.util.ArrayList;
import java.util.List;

import gson.client.InstructionList;
import gson.client.Order;
import gson.client.OrderMessage;
import gson.client.Params;

public class Main {

	private static final String TAG = "Main";

	public static List<Music> musicList = new ArrayList<>();

	public static void main(String[] args) {
//		List<Object> mList = new ArrayList<Object>();
//		recvOrderMessage(getPlayOrder());
//		recvMediaListMessage(sendMusicListMessage(mList));


	//	recvMediaListMessage(sendVideoListMessage());
	}

	public static String sendVideoListMessage(List<Object> mList) {
		//Video video1 = new Video(1, 26,"wangwu",  "whatName", "D://Music/whatName");
		//Video video2 = new Video(1, 24,"gouliu",  "whatName??", "D://Music/whatName");
		MediaList mediaList = new VideoList();
		//mediaList.addToList(video1);
		//mediaList.addToList(video2);
		mediaList.setType(0x002);
		mediaList.mList = mList;
		Messages messages = new Messages();
		messages.setResult(true);
		messages.setMsg(MediaList.packToJson(mediaList));
		return Messages.packToJson(messages);
	}

	public static String sendMusicListMessage(List<Object> mList) {
		//	Music music1 = new Music(0, 26,"李四",  "whatName", "D://Music/whatName");
		//	Music music2 = new Music(0, 25,"张三",  "thing", "D://Music/thing");
		MediaList mediaList = new MusicList();
		//	mediaList.addToList(music1);
		//	mediaList.addToList(music2);
		mediaList.setType(0x001);
		mediaList.mList = mList;
		Messages messages = new Messages();
		messages.setResult(true);
		messages.setMsg(MediaList.packToJson(mediaList));
		return Messages.packToJson(messages);
	}

	public static void recvMediaListMessage(String Jstr) {
		Messages messages = Messages.parseJson(Jstr);
		System.out.println(messages.getResult() + "");
		MediaList mediaList = new MediaList();
		MusicList musicList = new MusicList();
		VideoList videoList = new VideoList();
		JsonArray ja = MediaList.parseJson(mediaList, messages.getMsg());
		System.out.println("mediaList.mType is " + mediaList.getType());
		System.out.println("mediaList mList is " + mediaList.mList);
		if (mediaList.mType == MediaList.MEDIA_TYPE_MUSIC) {
			MusicList.parseJsonList(musicList, ja);
			for (Object o: musicList.getList()) {
				Music music = (Music) o;
				System.out.println("music is " + music.toString());
			}
		}else{
			VideoList.parseJsonList(videoList, ja);
			List<Video> vList = new ArrayList<Video>();
			for (Object o: videoList.getList()) {
				Video video = (Video) o;
				vList.add(video);
			}
		}
	}

//	public static String getMusicListOrder(int page) {
//		Params param = new Params();
//		param.addParam(InstructionList.PARAM.PAGE_NUM, page);
//		OrderMessage orderMsg = new OrderMessage();
//		orderMsg.setMode(InstructionList.MODE.MODE_MUSIC);
//		orderMsg.setOrderMsg(Params.packToJson(param));
//		Order order = new Order();
//		order.setCode(InstructionList.DATA_OP.GET_LIST);
//		order.setClientMsg(OrderMessage.packToJson(orderMsg));
//		return Order.packToJson(order);
//	}
//
//	public static String getVideoListOrder(int page) {
//		Params params = new Params();
//		params.addParam(InstructionList.PARAM.PAGE_NUM, page);
//		OrderMessage orderMsg = new OrderMessage();
//		orderMsg.setMode(InstructionList.MODE.MODE_VIDEO);
//		orderMsg.setOrderMsg(Params.packToJson(params));
//		Order order = new Order();
//		order.setCode(InstructionList.DATA_OP.GET_LIST);
//		order.setClientMsg(OrderMessage.packToJson(orderMsg));
//		return Order.packToJson(order);
//	}
//
//	public static String getPlayOrder() {
//		Params params = new Params();
//		params.addParam(InstructionList.PARAM.NULL, 0);
//		OrderMessage orderMsg = new OrderMessage();
//		orderMsg.setMode(InstructionList.MODE.MODE_NOTHING);
//		orderMsg.setOrderMsg(Params.packToJson(params));
//		Order order = new Order();
//		order.setCode(InstructionList.ORDER.ORDER_PLAY);
//		order.setClientMsg(OrderMessage.packToJson(orderMsg));
//		return Order.packToJson(order);
//	}
//
//	public static String getPauseOrder() {
//		Params params = new Params();
//		params.addParam(InstructionList.PARAM.NULL, 0);
//		OrderMessage orderMsg = new OrderMessage();
//		orderMsg.setMode(InstructionList.MODE.MODE_NOTHING);
//		orderMsg.setOrderMsg(Params.packToJson(params));
//		Order order = new Order();
//		order.setCode(InstructionList.ORDER.ORDER_PAUSE);
//		order.setClientMsg(OrderMessage.packToJson(orderMsg));
//		return Order.packToJson(order);
//	}
//
//	public static String getProviousOrder() {
//		Params params = new Params();
//		params.addParam(InstructionList.PARAM.NULL, 0);
//		OrderMessage orderMsg = new OrderMessage();
//		orderMsg.setMode(InstructionList.MODE.MODE_NOTHING);
//		orderMsg.setOrderMsg(Params.packToJson(params));
//		Order order = new Order();
//		order.setCode(InstructionList.ORDER.ORDER_PROVIOUS);
//		order.setClientMsg(OrderMessage.packToJson(orderMsg));
//		return Order.packToJson(order);
//	}
//
//	public static String getNextOrder() {
//		Params params = new Params();
//		params.addParam(InstructionList.PARAM.NULL, 0);
//		OrderMessage orderMsg = new OrderMessage();
//		orderMsg.setMode(InstructionList.MODE.MODE_NOTHING);
//		orderMsg.setOrderMsg(Params.packToJson(params));
//		Order order = new Order();
//		order.setCode(InstructionList.ORDER.ORDER_NEXT);
//		order.setClientMsg(OrderMessage.packToJson(orderMsg));
//		return Order.packToJson(order);
//	}
//	//服务端发给客户端
//	public static String sendCurroutTimeOrder() {
//		Params params = new Params();
//		params.addParam(InstructionList.PARAM.NULL, 0);
//		OrderMessage orderMsg = new OrderMessage();
//		orderMsg.setMode(InstructionList.MODE.MODE_NOTHING);
//		orderMsg.setOrderMsg(Params.packToJson(params));
//		Order order = new Order();
//		order.setCode(InstructionList.ORDER.ORDER_GETDURATION);
//		order.setClientMsg(OrderMessage.packToJson(orderMsg));
//		return Order.packToJson(order);
//	}
//	//客户端向服务端发送ViewList点击的currentMediaList
//	public static String sendClickItemIndex(int position) {
//		Params params = new Params();
//		params.addParam(InstructionList.PARAM.CURRENT_MEDIAINDEX, position);
//		OrderMessage orderMsg = new OrderMessage();
//		orderMsg.setMode(InstructionList.MODE.MODE_NOTHING);
//		orderMsg.setOrderMsg(Params.packToJson(params));
//		Order order = new Order();
//		order.setCode(InstructionList.ORDER.ORDER_GETDURATION);
//		order.setClientMsg(OrderMessage.packToJson(orderMsg));
//		return Order.packToJson(order);
//	}
//
//	public static String getIsPlayingOrder() {
//		Params params = new Params();
//		params.addParam(InstructionList.PARAM.NULL, 0);
//		OrderMessage orderMsg = new OrderMessage();
//		orderMsg.setMode(InstructionList.MODE.MODE_NOTHING);
//		orderMsg.setOrderMsg(Params.packToJson(params));
//		Order order = new Order();
//		order.setCode(InstructionList.ORDER.ORDER_ISPLAYING);
//		order.setClientMsg(OrderMessage.packToJson(orderMsg));
//		return Order.packToJson(order);
//
//	}

	//通用发送命令
	//发送得到列表命令时，需要传递四个参数：什么命令(ORDER), 媒体模式(MODE), 得到的页数(PARAM)，页数(value)
	//发送控制类型命令时，需要传递一个参数，ORDER(play, pause, ...),其余参数置空，具体参考InstructionList指令集。
	//发送媒体模式类型命令时，需要传递一个参数，MODE(music, video),其余参数置空。
	public static String sendOrder(int ORDER, int MODE, String param_name, Object param_value) {
		Params params = new Params();
		params.addParam(param_name, param_value);
		return sendOrder(ORDER, MODE, params);
	}

	public static String sendOrder(int ORDER, int MODE, Params params) {
		OrderMessage orderMsg = new OrderMessage();
		orderMsg.setMode(MODE);
		orderMsg.setOrderMsg(Params.packToJson(params));
		Order order = new Order();
		order.setCode(ORDER);
		order.setClientMsg(OrderMessage.packToJson(orderMsg));
		return Order.packToJson(order);
	}
	//通用发送列表信息
	//type类型有两种: 0x001音乐列表， 0x002视频列表。
	public static String sendListMessage(int type, List<Object> mList) {
		MediaList mediaList = new MusicList();
		mediaList.setType(type);
		mediaList.mList = mList;
		Messages messages = new Messages();
		messages.setResult(true);
		messages.setMsg(MediaList.packToJson(mediaList));
		return Messages.packToJson(messages);
	}

	//解析通用命令并操作
	public static void recvOrderMessage(String Jstr) {
		Order order = Order.parseJson(Jstr);
//		System.out.println("命令为 "+order.code);
//		System.out.println("命令信息为 " +order.clientMsg);
		OrderMessage orderMsg = OrderMessage.parseJson(order.clientMsg);
//		System.out.println("播放模式为" + orderMsg.mode);
		Params param = Params.parseToJson(orderMsg.orderMsg);
		//
		//System.out.println("param 1 is " + param.mParams.get(1).value);

		switch(order.code) {
			case InstructionList.ORDER.ORDER_PLAY:
				Log.d("Main", "播放 ");
				//此处执行播放功能
				break;
			case InstructionList.ORDER.ORDER_PAUSE:
				Log.d("Main", "暂停 ");
				//此处执行暂停功能
				break;
			case InstructionList.ORDER.ORDER_PREVIOUS:
				Log.d("Main", "上一个 ");
				//此处执行上一个
				break;
			case InstructionList.ORDER.ORDER_NEXT:
				Log.d("Main", "下一个 ");
				//此处执行下一个
				break;
			case InstructionList.ORDER.ORDER_DONOTHING:
				Log.d("Main", "无效命令 ");
				//无效命令
				break;
			case InstructionList.ORDER.ORDER_ISPLAYING:
				Log.d("Main", "是否正在播放 ");
				//是否正在播放
				break;
			case InstructionList.ORDER.ORDER_GETDURATION:
				Log.d("Main", "得到当前歌曲进度 ");
				//得到当前歌曲进度
				break;
//			case InstructionList.DATA_OP.GET_LIST:
//				Log.d("Main", "要发送列表 ");
//				//此处执行发送相应媒体列表
//				switch (orderMsg.mode) {
					case InstructionList.MODE.MODE_MUSIC:
						Log.d("Main", "服务端收到客户端请求，要发送音乐列表 ");
						//发送音乐列表并切换到音乐播放界面
						break;
					case InstructionList.MODE.MODE_VIDEO:
						Log.d("Main", "服务端收到客户端请求，要发送视频列表 ");
						//发送视频列表并切换到视频播放界面
						break;
//					default:
//						break;
//				}
//				break;
			default:
				break;
		}
	}

	//解析列表信息并操作
	public static void recvListMessage(String Jstr) {
		Messages messages = Messages.parseJson(Jstr);
		System.out.println(messages.getResult() + "-----");
		MediaList mediaList = new MediaList();


		JsonArray ja = MediaList.parseJson(mediaList, messages.getMsg());
		if (mediaList.mType == MediaList.MEDIA_TYPE_MUSIC) {
			Log.d("Main", "音乐ListView适配器 ");
			//可以在此处设置音乐ListView适配器

			//getMusicList(ja);  	//此方法返回List<Music>
			musicList = getMusicList(ja);
			Log.d(TAG, "recvListMessage: " + musicList.get(1).getMusicName());
		}else{
			Log.d("Main", "视频ListView适配器 ");
			//可以在此处设置视频ListView适配器
			getVideoList(ja);	//此方法返回List<Video>
		}
	}

	public static List<Music> getMusicList(JsonArray ja) {
		MusicList musicList = new MusicList();
		MusicList.parseJsonList(musicList, ja);
		List<Music> MList = new ArrayList<>();
		for (Object o: musicList.getList()) {
			Music music = (Music) o;
			MList.add(music);
		}
		return MList;
	}

	public static List<Video> getVideoList(JsonArray ja) {
		VideoList videoList = new VideoList();
		VideoList.parseJsonList(videoList, ja);
		List<Video> VList = new ArrayList<>();
		for (Object o: videoList.getList()) {
			Video video = (Video) o;
			VList.add(video);
		}
		return VList;
	}

}

